package com.socket {
	import com.debug.TDebug;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;

	public class SocketCommand extends EventDispatcher{
		public var mySocket:Socket;
		
		public function SocketCommand() {
			initSocket();
		}
		private function initSocket():void{
			mySocket = new Socket();
			mySocket.addEventListener(Event.CONNECT,onConnectHandler);
			mySocket.addEventListener(Event.CLOSE,onCloseHandler);
		}
		public function socketConnHandler(parm:String,port:uint):void{
			if(!mySocket.connected){
				this.log("正在连接Socket...");
				this.log("socket连接地址为:" + parm + "port:" + port);
				mySocket.connect(parm,port);
				mySocket.addEventListener(ProgressEvent.SOCKET_DATA,dataHandler);
				mySocket.addEventListener(IOErrorEvent.IO_ERROR,ioError);
				mySocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityHandler);
			}
		}
		public function closeSocketFun():void{
			this.log("Socket连接关闭");
			if(this.mySocket!=null&&mySocket.connected){
				mySocket.close();
			}
		}
		
		private function onConnectHandler(e:Event):void{
			
		}
		private function onCloseHandler(e:Event):void{
			
		}
		private function dataHandler(e:ProgressEvent):void{
			
		}
		private function ioError(e:IOErrorEvent):void{
			
		}
		private function securityHandler(e:SecurityErrorEvent):void{
			
		}
		
		
		//输出日志
		private function log(... arg):void{
			TDebug.trace(arg);
		}
	}
}